import 'package:flutter/material.dart';
import 'package:flutter_axrjc/routes.dart';
import 'package:flutter_axrjc/style.dart';
import 'package:flutter_axrjc/util/Util.dart';
import 'package:flutter_axrjc/widget/common_widget.dart';
import 'package:flutter_axrjc/widget/dialog.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'logic.dart';

class LightDetailPage extends StatelessWidget {
  const LightDetailPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final logic = Get.find<LightDetailLogic>();
    final state = Get.find<LightDetailLogic>().state;
    return Scaffold(
      backgroundColor: const Color(0xffF3F6F9),
      appBar: AppBar(
        centerTitle: true,
        title: const Text("查看灯具"),
      ),
      body: SmartRefresher(
        header: reFreshHeader(),
        footer: loadFooter(),
        enablePullDown: true,
        onRefresh: () {
          logic.getLog();
        },
        controller: logic.refreshController,
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              InkWell(
                onTap: () {
                  Get.toNamed(MainRoutes.editLight, arguments: logic.bean);
                },
                child: Padding(
                  padding: const EdgeInsets.all(15),
                  child: Container(
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(10)),
                    child: Column(
                      children: [
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              const Text(
                                "灯具名称",
                                style: TextStyle(color: Color(0xff999999)),
                              ),
                              Text("${logic.bean.fixtureName}"),
                            ],
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              const Text(
                                "检测设备",
                                style: TextStyle(color: Color(0xff999999)),
                              ),
                              Text("${logic.bean.fixtureBrand}"),
                            ],
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              const Text(
                                "照度数值",
                                style: TextStyle(color: Color(0xff999999)),
                              ),
                              Text(
                                  "${(logic.bean.brightnessAdjustment?.isEmpty == true || logic.bean.brightnessAdjustment == null) ? 0 : '${logic.bean.brightnessAdjustment}'}lux"),
                            ],
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              const Text(
                                "购买日期",
                                style: TextStyle(color: Color(0xff999999)),
                              ),
                              Text("${logic.bean.installationDate}"),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 20),
                child: Obx(() {
                  return GridView.builder(
                    shrinkWrap: true,
                    physics: const NeverScrollableScrollPhysics(),
                    itemCount: state.imgList.length,
                    gridDelegate:
                        const SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount: 3,
                            crossAxisSpacing: 5,
                            mainAxisSpacing: 5,
                            childAspectRatio: 1),
                    itemBuilder: (BuildContext context, int index) {
                      var bean = state.imgList[index];
                      return InkWell(
                        onTap: () {
                          showPicPreview(["${bean.filePath}"]);
                        },
                        child: Container(
                          decoration: BoxDecoration(
                              color: Colors.white,
                              boxShadow: [
                                BoxShadow(
                                    color: Colors.grey.withAlpha(20),
                                    blurRadius: 5)
                              ],
                              borderRadius: BorderRadius.circular(5),
                              image: DecorationImage(
                                  image: NetworkImage("${bean.filePath}"),
                                  fit: BoxFit.cover)),
                        ),
                      );
                    },
                  );
                }),
              ),
              const Padding(
                padding: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
                child: Text(
                  "检测记录",
                  style: TextStyle(fontSize: 16),
                ),
              ),
              const SizedBox(
                height: 10,
              ),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 15),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    InkWell(
                      onTap: () {
                        Get.toNamed(MainRoutes.reportMonth);
                      },
                      child: const Padding(
                        padding: EdgeInsets.all(8.0),
                        child: Text(
                          "查看月度报告",
                          style: TextStyle(fontSize: 12, color: orangeColor),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(15),
                child: Obx(() {
                  return ListView.builder(
                    padding: const EdgeInsets.symmetric(horizontal: 10),
                    shrinkWrap: true,
                    physics: const NeverScrollableScrollPhysics(),
                    itemCount: state.list.length,
                    itemBuilder: (BuildContext context, int index) {
                      var bean = state.list[index];
                      return ExpansionTile(
                        leading: Text("${bean.key}"),
                        expandedAlignment: Alignment.centerLeft,
                        title: Container(),
                        children: List.generate(bean.value.length, (index1) {
                          var bean1 = bean.value[index1];
                          return Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Container(
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 10, vertical: 10),
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(10),
                                  boxShadow: [
                                    BoxShadow(
                                        color: Colors.grey.withAlpha(20),
                                        spreadRadius: 5,
                                        blurRadius: 5)
                                  ]),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    Util.date2String(
                                            Util.string2Date(
                                                "${bean1.detectionStartTime}")!,
                                            pattern: "HH:mm") ??
                                        '',
                                    style: const TextStyle(color: Colors.grey),
                                  ),
                                  const SizedBox(
                                    height: 10,
                                  ),
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Row(
                                        children: [
                                          const Text("亮度"),
                                          const SizedBox(
                                            width: 5,
                                          ),
                                          Text(
                                            "${bean1.detectionAverageValue}lux",
                                            style: const TextStyle(
                                                color: orangeColor),
                                          ),
                                        ],
                                      ),
                                      // Row(
                                      //   children: [
                                      //     const Text("色温"),
                                      //     const SizedBox(
                                      //       width: 5,
                                      //     ),
                                      //     Text(
                                      //       "${bean1.colorTemp ?? 0}",
                                      //       style: const TextStyle(
                                      //           color: orangeColor),
                                      //     ),
                                      //   ],
                                      // )
                                    ],
                                  ),
                                  const SizedBox(
                                    height: 10,
                                  ),
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text(
                                        "${bean1.mobileBrand}-${bean1.mobileModel}",
                                        style:
                                            const TextStyle(color: Colors.grey),
                                      ),
                                      Text(
                                        "检测耗时 ${getTimes("${bean1.detectionStartTime}", "${bean1.detectionEndTime}")}s",
                                        style:
                                            const TextStyle(color: Colors.grey),
                                      ),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          );
                        }),
                      );
                    },
                  );
                }),
              )
            ],
          ),
        ),
      ),
    );
  }

  int getTimes(String s, String t) {
    return (Util.string2Date(s)!.difference(Util.string2Date(t)!).inSeconds)
        .abs();
  }
}
